package view;

import adaptors.TabReference;
import controller.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.BorderFactory;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;

import tools.Tab;
import tools.TabIO;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class View extends javax.swing.JFrame {
	
	private static final long serialVersionUID = 1L;
	public Controller controller;// = new Controller();
	private JFrame loadingWindow;
	
	private JPanel searchPanel;
	private JTabbedPane jTabbedPane1;
	private JPanel localTabsPanel;
	private JPanel preferencesPanel;
	private JFrame searchingWindow;
	private int[] columnWidths = new int[4];
	
	
//	{
		//Set Look & Feel
//		try {
//			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
//		} catch(Exception e) {
//			e.printStackTrace();
//		}
//	}


	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				View inst = new View();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public View() {
		super();
		controller = new Controller();
		controller.setView(this);
		controller.loadFilesAndSettings();
		initGUI();
		this.updateLocalTabs();
	}
	
	private void initGUI() {
		controller.setView(this);

		// PRELOAD WINDOW!!
		loadingWindow = new JFrame();
		loadingWindow.setUndecorated(true);
		
		JLabel ll = new JLabel("Loading...", new ImageIcon(getClass().getClassLoader().getResource("view/icons/loading24.gif")), JLabel.LEFT);
		ll.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		ll.setPreferredSize(new Dimension(110, 25));

		JPanel p = new JPanel(new BorderLayout());
		p.add(ll, BorderLayout.CENTER);
		
		loadingWindow.add(p);
		loadingWindow.pack();
		loadingWindow.setLocationRelativeTo(null);
		loadingWindow.setVisible(true);
		loadingWindow.repaint();
		
		// PRELOAD WINDOW!!
		searchingWindow = new JFrame();
		searchingWindow.setUndecorated(true);
		
		JLabel l2 = new JLabel("Searching...", new ImageIcon(getClass().getClassLoader().getResource("view/icons/find-icon.png")), JLabel.LEFT);
		l2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		l2.setPreferredSize(new Dimension(130, 25));

		JPanel p2 = new JPanel(new BorderLayout());
		p2.add(l2, BorderLayout.CENTER);
		
		searchingWindow.add(p2);
		searchingWindow.pack();
		searchingWindow.setLocationRelativeTo(null);
		searchingWindow.setVisible(true);
		searchingWindow.repaint();

		try {
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			this.setTitle("Guitar Scraper");
			this.setLocale(new java.util.Locale("en"));
			this.setPreferredSize(new java.awt.Dimension(796, 483));
			{
				jTabbedPane1 = new JTabbedPane();
				getContentPane().add(jTabbedPane1);
				jTabbedPane1.setPreferredSize(new java.awt.Dimension(782, 453));

				localTabsPanel = new LocalTabsPanel(this);
				jTabbedPane1.addTab("Local Tabs", localTabsPanel);
				
				searchPanel = new SearchPanel(this);
				jTabbedPane1.addTab("Search Tabs", searchPanel);
				
				preferencesPanel = new PreferencesPanel(this);
				jTabbedPane1.addTab("Preferences", preferencesPanel);
			}

			pack();
			this.setSize(796, 483);

		} catch (Exception e) {
			e.printStackTrace();
		}		

	}
	
	public void internetSearchResults(ArrayList<Tab> results)	{
		if( results == null )	{
			JOptionPane.showMessageDialog(null, "No search results found.");			
			((SearchPanel)searchPanel).clearSearchTable();
		}
		else	{
			((SearchPanel)searchPanel).fillTable(results);			
		}
	}

	public void localSearchResults()	{
		
	}
		
	public void updateLocalTabs()	{
		((LocalTabsPanel)localTabsPanel).updateBandTree();
	}
	
	public void showLoadingWindow(boolean state)	{
		loadingWindow.setVisible(state);
	}

	public void showSearchingWindow(boolean state) {
		searchingWindow.setVisible(state);
	}
	
	public String getTabRootPath()	{
		return controller.prefs.getTabRootDirectory();
	}
	
	public void editTab(TabReference t)	{
		Tab tab = TabIO.readTabFile(controller.prefs.getTabRootDirectory()+t.getPath());
		EditTabView etv = new EditTabView(this, tab);
		etv.pack();
		etv.setVisible(true);
	}
	
	// Controller relays
	
	public boolean deleteTab(TabReference tabRef)	{
		return controller.deleteTabFromLibrary(tabRef);
	}
	
	public boolean saveTab(Tab tab) {
		if( controller.saveTab(tab) )
			return controller.addTabToLibrary(tab);

		return false;
	}

	public void displayTab(Tab tab) {
		TabView tv = new TabView(this, tab);
		tv.setVisible(true);
	}

	public int[] getSearchColumnWidths()	{
		return controller.prefs.getSearchColumnWidths();
	}
	
	public void setSearchColumnWidths()	{
		((SearchPanel)searchPanel).setColumnWidths(columnWidths);
	}

	public void saveColumnWidths(int[] widths) {
		controller.saveColumnWidths(widths);
		
	}

	public void savePreferences() {
		// TODO make all prefs save this way, only when the user hits apply
		// TODO make revert method for reverting changes on the prefsPanel
		controller.savePreferences();
	}

	public void saveLocalTabSplitPosition(int position) {
		controller.saveLocalTabSplitPosition(position);
	}

	public void setLocalTabSplitPosition(int position)	{
		((LocalTabsPanel)localTabsPanel).setTabSplitPosition(position);
	}

	public int getLocalTabSplitPosition() {
		return controller.prefs.getLocalTabSplitPosition();
	}
	
	public Font getPreferredFont()	{
		return controller.prefs.getPreferredFont();
	}
	
	public void setPreferredFont(Font f)	{
		controller.prefs.setPreferredFont(f);
	}
	
	public Color getPrefFGColor()	{
		return controller.prefs.getTextForegroundColor();
	}

	public Color getPrefBGColor() {
		return controller.prefs.getTextBackgroundColor();
	}
	
	public void setPrefFGColor(Color c)	{
		controller.prefs.setTextForegroundColor(c);
	}

	public void setPrefBGColor(Color c)	{
		controller.prefs.setTextBackgroundColor(c);
	}
}
    
